Become a Pro / website · becomeapro.ai / src / app / [area] / [field] / [lesson] / page.tsx
| 1 | import type { Metadata } from "next"; |
| 2 | import Link from "next/link"; |
| 3 | import { notFound } from "next/navigation"; |
| 4 | import { MDXRemote } from "next-mdx-remote/rsc"; |
| 5 | import remarkGfm from "remark-gfm"; |
| 6 | import { mdxComponents } from "@/components/mdx/mdx-components"; |
| 7 | import { SaveLessonButton } from "@/components/save-lesson-button"; |
| 8 | import ShareButton from "@/components/share-button"; |
| 9 | import { getArea, getField } from "@/lib/catalog"; |
| 10 | import { formatDate, getAllLessons, getFieldLessons, getLesson, lessonKey, lessonPath } from "@/lib/lessons"; |
| 11 | import { pageMetadata, SITE_URL } from "@/lib/seo"; |
| 12 | |
| 13 | export const dynamicParams = false; |
| 14 | |
| 15 | export function generateStaticParams() { |
| 16 | return getAllLessons().map((l) => ({ area: l.area, field: l.field, lesson: l.slug })); |
| 17 | } |
| 18 | |
| 19 | type Props = { params: Promise<{ area: string; field: string; lesson: string }> }; |
| 20 | |
| 21 | export async function generateMetadata({ params }: Props): Promise<Metadata> { |
| 22 | const { area, field, lesson } = await params; |
| 23 | const l = getLesson(area, field, lesson); |
| 24 | return l ? pageMetadata({ title: l.title, description: l.description }) : {}; |
| 25 | } |
| 26 | |
| 27 | export default async function LessonPage({ params }: Props) { |
| 28 | const { area: areaSlug, field: fieldSlug, lesson: slug } = await params; |
| 29 | const lesson = getLesson(areaSlug, fieldSlug, slug); |
| 30 | const area = getArea(areaSlug); |
| 31 | const field = getField(areaSlug, fieldSlug); |
| 32 | if (!lesson || !area || !field) notFound(); |
| 33 | |
| 34 | const siblings = getFieldLessons(field.slug); |
| 35 | const index = siblings.findIndex((l) => l.slug === lesson.slug); |
| 36 | const next = siblings[index + 1]; |
| 37 | |
| 38 | return ( |
| 39 | <article className="page"> |
| 40 | <p className="crumbs"> |
| 41 | <Link href="/">Home</Link> / <Link href={`/${area.slug}`}>{area.name}</Link> /{" "} |
| 42 | <Link href={`/${area.slug}/${field.slug}`}>{field.name}</Link> |
| 43 | </p> |
| 44 | <h1 className="text-3xl font-bold leading-tight md:text-4xl">{lesson.title}</h1> |
| 45 | <p className="mt-3 text-xl">{lesson.description}</p> |
| 46 | <p className="mt-3 text-[15px] text-[var(--ash)]"> |
| 47 | {lesson.level} · {lesson.minutes} min read · Updated {formatDate(lesson.updated)} |
| 48 | </p> |
| 49 | <div className="mt-4 flex flex-wrap items-center gap-3"> |
| 50 | <SaveLessonButton lessonKey={lessonKey(lesson)} /> |
| 51 | <ShareButton small url={`${SITE_URL}${lessonPath(lesson)}`} title={lesson.title} /> |
| 52 | </div> |
| 53 | |
| 54 | <div className="lesson mt-8"> |
| 55 | <MDXRemote source={lesson.body} components={mdxComponents} options={{ mdxOptions: { remarkPlugins: [remarkGfm] } }} /> |
| 56 | </div> |
| 57 | |
| 58 | <nav aria-label="Next" className="mt-14 border-t border-border pt-6"> |
| 59 | {next ? ( |
| 60 | <p> |
| 61 | Next lesson:{" "} |
| 62 | <Link href={lessonPath(next)} className="font-semibold underline"> |
| 63 | {next.title} |
| 64 | </Link> |
| 65 | </p> |
| 66 | ) : ( |
| 67 | <p> |
| 68 | Back to <Link href={`/${area.slug}/${field.slug}`} className="underline">{field.name}</Link> |
| 69 | </p> |
| 70 | )} |
| 71 | </nav> |
| 72 | </article> |
| 73 | ); |
| 74 | } |